package br.com.lazyprogrammer.worldsoccermanager.network.task;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;

import br.com.lazyprogrammer.worldsoccermanager.network.Request;
import br.com.lazyprogrammer.worldsoccermanager.network.URLParams;
import br.com.lazyprogrammer.worldsoccermanager.network.builders.URLBuilder;
import br.com.lazyprogrammer.worldsoccermanager.network.interfaces.TeamListener;
import br.com.lazyprogrammer.worldsoccermanager.network.model.Team;
import br.com.lazyprogrammer.worldsoccermanager.system.message.Message;
import br.com.lazyprogrammer.worldsoccermanager.util.Parser;

public class CreateTeamTask extends AsyncTask<Team, Void, Request.Response> {
	
	private Context context;
	private Request.Callback callback;
	private TeamListener listener;
	
	public CreateTeamTask(Context context, Request.Callback callback, TeamListener listener) {
		this.context	= context;
		this.callback	= callback;
		this.listener	= listener;
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		callback.onStart(getClass());
	}

	@Override
	protected Request.Response doInBackground(Team... param) {
		// TODO Auto-generated method stub
		Parser parser	= new Parser();
		Request.Response status;
		try {
			if (isCancelled())
				cancel(true);
			JSONObject parent	= new Request().post(new URLBuilder().buildUrlGame(URLBuilder.Type.Team), new URLParams().buildCreateTeamParams(null, param[0]));
			status	= Request.Response.values()[parser.parseResponse(parent)];
			if (status.equals(Request.Response.Success))
				listener.onTeamCreated(param[0]);
		} catch (SocketTimeoutException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.SocketTimeout;
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.UnsupportedEncoding;
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.ClientProtocol;
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.IllegalState;
			e.printStackTrace();
		} catch (NullPointerException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.NullPointer;
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.IO;
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.JSON;
			e.printStackTrace();
		}
		return status;
	}
	
	@Override
	protected void onPostExecute(Request.Response result) {
		if (isCancelled())
			callback.onCancel(getClass());
		else {
			if (result.equals(Request.Response.Success))
				callback.onSuccess(getClass(), result);
			else
				callback.onError(getClass(), result, Message.getException(context, result));
		}
	}

}
